package com.cms.server.resolver.plugins.sctz;

import java.sql.ResultSet;
import java.util.Date;

import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.form.server.resolver.util.SYS_MDL_VAL;
import com.efounder.sql.JStatement;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;

import dwz.common.util.DateUtil;

public class CMSCheckCountersignPreNodeRetake extends FormDataResolverAdapter {

	//初始化FormDataModel
	public void prepareSaveForm(JStatement stmt, EFFormDataModel model, JParamObject PO) throws Exception {		
		String                            strSql = "";
		EFDataSet                     flowNodeDS = null;
		EFRowSet                      nodeRowset = EFRowSet.getInstance();
		ResultSet                             rs = null;
		EFDataSet                            eds = model.getBillDataSet();
		EFRowSet                             ers = eds.getRowSet(0);
	    String                            F_KJQJ = ers.getString(PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ), "");
	    String                            F_DJBH = ers.getString(PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH), null);
	    String                           FLOW_ID = PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "");
	    String                          errorMsg = "";
	    String                     RESR_IN_CAUSE = PO.GetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "");
	    
	    //如果不是提交操作，则返回
		if(!RESR_IN_CAUSE.equals("retake")) return;
		
		//获取流程节点
		strSql = "select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
		       + " FLOW_ID = '" + FLOW_ID + "' and NODE_TAG = '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "' order by OP_ID DESC";
		rs = stmt.executeQuery(strSql);
		flowNodeDS = DataSetUtils.resultSet2DataSet(rs, flowNodeDS);
		nodeRowset = flowNodeDS.getRowSet(0);
		
		if(nodeRowset == null) {
			throw new Exception("<br>单据已经结束流程，请重新打开单据！");
		}
		
		strSql = "select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
		       + " FLOW_ID = '" + FLOW_ID + "' and POP_ID = '" + nodeRowset.getString("OP_ID", "") + "' and RESR_STATUS = 'processed'";
		rs = stmt.executeQuery(strSql);
		flowNodeDS = EFDataSet.getInstance();
		flowNodeDS = DataSetUtils.resultSet2DataSet(rs, flowNodeDS);
		if(flowNodeDS.getRowCount() > 0) {
			for(int i = 0; i < flowNodeDS.getRowCount(); i++) {
				errorMsg += "<br>部门【" + flowNodeDS.getRowSet(i).getString("TASK_TO_UNIT_NAME"	, "") + "】、";
			}
			errorMsg = errorMsg.substring(0, errorMsg.lastIndexOf("、"));
			errorMsg += "已经提交，不能取回单据！";
			throw new Exception(errorMsg);
		}
		
		strSql = "select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
	       + " FLOW_ID = '" + FLOW_ID + "' and RESR_STATUS = 'waiting'";
		rs = stmt.executeQuery(strSql);
		flowNodeDS = EFDataSet.getInstance();
		flowNodeDS = DataSetUtils.resultSet2DataSet(rs, flowNodeDS);
		if(flowNodeDS.getRowCount() > 0) {
			for(int i = 0; i < flowNodeDS.getRowCount(); i++) {
				errorMsg += "<br>部门【" + flowNodeDS.getRowSet(i).getString("F_CHAR20"	, "") + "】、";
			}
			errorMsg = errorMsg.substring(0, errorMsg.lastIndexOf("、"));
			errorMsg += "已经提交，不能取回单据！";
			throw new Exception(errorMsg);
	}
	}
}
